package model

import (
	"common/dao/model"
	"context"
	"fmt"
	"github.com/go-deepseek/deepseek"
	"github.com/go-deepseek/deepseek/request"
	"gorm.io/gorm"

	"gateway/internal/svc"
	"gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ChatTitleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewChatTitleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatTitleLogic {
	return &ChatTitleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ChatTitleLogic) ChatTitle(req *types.ChatTitle) (*types.ChatTitleRep, error) {
	var (
		modelId   = 2
		modelData = model.Model{}
		resp      = types.ChatTitleRep{}
	)
	err := l.svcCtx.DB.Table(model.TableNameModel).Transaction(func(tx *gorm.DB) error {
		return tx.Where("id = ?", modelId).Find(&modelData).Error
	})
	cli, _ := deepseek.NewClient(modelData.APIKey)

	inputMessage := req.Content // set your input message
	chatReq := &request.ChatCompletionsRequest{
		Model:  deepseek.DEEPSEEK_CHAT_MODEL,
		Stream: false,
		Messages: []*request.Message{
			{
				Role:    "user",
				Content: inputMessage,
			},
		},
	}
	chatResp, err := cli.CallChatCompletionsChat(context.Background(), chatReq)
	if err != nil {
		fmt.Println("error => ", err)
		return nil, err
	}
	fmt.Printf("output message => %s\n", chatResp.Choices[0].Message.Content)
	resp.Message = chatResp.Choices[0].Message.Content
	return &resp, nil

}
